import networkx as nx
import matplotlib.pyplot as plt

# 选择具有代表性的关键词
keywords = [
    "低脂", "高蛋白", "低糖", "低卡", "无添加", "瘦身", "代餐", "抗氧化", "营养均衡", "健康",
    "膳食纤维", "维生素", "酥脆", "果仁", "燕麦", "健身人士", "即食", "价格", "味道", "口感"
]

# 定义关键词之间的关系
relations = [
    ("低脂", "低卡"), ("低脂", "瘦身"), ("低卡", "代餐"), ("低糖", "低卡"), ("低糖", "无添加"),
    ("高蛋白", "健身人士"), ("高蛋白", "代餐"), ("高蛋白", "膳食纤维"), ("代餐", "营养均衡"),
    ("抗氧化", "维生素"), ("抗氧化", "健康"), ("营养均衡", "健康"), ("健康", "价格"),
    ("健康", "味道"), ("健康", "口感"), ("膳食纤维", "燕麦"), ("酥脆", "果仁"),
    ("即食", "便利"), ("价格", "包装"), ("味道", "口感")
]

# 创建图
G = nx.Graph()
G.add_nodes_from(keywords)
G.add_edges_from(relations)

# 绘制语义网络图
plt.figure(figsize=(10, 6))
pos = nx.spring_layout(G, seed=42)  # 设定布局
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10, font_family='SimHei')
plt.title("功能性零食关键词语义网络")
plt.show()